<div class="layuimini-main">

    <form class="layui-form layuimini-form" id="addAgvForm">
        <div class="layui-form-item">
            <label class="layui-form-label required">机器人名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="AGV名称不能为空" placeholder="请输入AGV名称" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">机器人编号</label>
            <div class="layui-input-block">
                <input type="text" name="Id" id="ID" lay-verify="required|checkonly" placeholder="请输入机器人编号" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">机器人IP</label>
            <div class="layui-input-block">
                <input type="text" name="ip" id="IP" lay-verify="required|ip|repeat" lay-reqtext="IP地址不能为空" placeholder="请输入IP地址" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">机器人Port</label>
            <div class="layui-input-block">
                <input type="number" name="port" placeholder="请输入AGV_Port" value=7273 class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所在区域</label>
            <div class="layui-input-block">
                <input type="text" name="region" placeholder="请输入所在区域" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">可背负物料</label>
            <div class="layui-input-block">
                <input type="text" name="payloads" placeholder="请输入可背负的物料型号,多个物料型号用','隔开" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所在楼层</label>
            <div class="layui-input-block">
                <input type="number" name="floor" placeholder="请输入所在楼层" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'table'], function() {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;

        form.verify({
            //验证IP是否正确
            ip: [
                /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/,
                'IP地址不符合规则'
            ],
            repeat: function (value) { //value：表单的值、item：表单的DOM对象
                var checkResult = "";

                $.ajax({
                    url: "api/Robot/Check/ip=" + value,
                    type: "GET",
                    async: false,
                    success: function (result) {
                        if (result.data) {
                            checkResult= "该IP已经被占用!";
                        }
                    },
                    error: function () {
                    }
                });
                return checkResult;
            },

            checkonly: function (value) { //value：表单的值、item：表单的DOM对象
                var checkResult = "";
                $.ajax({
                    url: "api/Robot/Check/id=" + value,
                    type: "GET",
                    //data: JSON.stringify(ip),
                    async: false,
                    success: function (result) {
                        if (result.data) {
                            checkResult= "该ID已经被占用!";
                        }
                    },
                    error: function () {
                    }
                });
                return checkResult;
            }
        });
        //监听提交
        form.on('submit(saveBtn)', function(data) {
            $.ajax({
                url: '/api/Robot/Create',
                method: 'post',
                contentType: 'application/json',
                cache: false,
                data: JSON.stringify(data.field),
                success: function(res) {
                    console.log(res);
                    if (res.code === 200) {
                        var index = layer.confirm('添加成功!是否继续添加?', {
                            btn: ['是', '否'],
                            yes: function () {
                                layer.close(index); 
                                $("#addAgvForm")[0].reset();
                                layui.form.render();
                            },
                           btn2: function () {
                               layer.closeAll();
                           }
                            
                        });

        }                
                    else            
                        alert(res.msg);   
                },              
                error:function (data) {

                    console.log(data);
                    layer.msg('添加失败!', {icon:5}); 
                     
                }           
            }) ;
            return false;
        });

    });
</script>